import commonMethod from "@/common/commonMethod";
import Marker from "./Marker";
import markerCodes from "./MarkerCode";
import commonConstants from "@/common/commonConstants";

class MarkerManager{

    constructor(v,configs,monitorManager){
        this.markers = [];
        this.markerCodes = [];
        this.monitorManager = monitorManager

        this.curViewer = v;
        this.markerlayer = false;
        this.attributePlane = false;
        this._handlerConfigs(configs);

        this._initMakerCOde();
        this.layerManager = undefined;

        this.monitorManager.setMarkerManager(this);
    }

    //处理一些配置信息
    _handlerConfigs(configs){
        //没有就不处理
        configs.forEach(config => {
            if(config.getKey() == "attributePlane"){
                this.attributePlane = config.getValue();
            }
        });
    }

    _initMakerCOde(){
       this.markerCodes = markerCodes;
    }

    setLayerManager(lm){
        this.layerManager = lm;
    }

    setMonitorManager(manager){
        let _this= this;
        _this.monitorManager = manager;
        _this.monitorManager.setMarkerManager(_this)
    }

    getMonitorManager(){
        let _this = this;
        return _this.monitorManager;
    }

    getMarkers(){
        let _this = this;
        return _this.markers;
    }

    /**
     * 通过ID 获得 标绘
     * @returns {Marker}
     */
    getMarkerById(markerId){
        const _this = this;
        return commonMethod.findElementFromArray(_this.markers,marker => marker.getMarkerId() === markerId);
    }

    getMarkerCodes(){
        let _this = this;
        return _this.markerCodes;
    }

     /**
     * 通过标绘代号获得标绘集合,以code的集合获得标绘集合
     * @param markerCodes
     * @return {[]}
     */
     getMarkersByCode(...markerCodes){
        let _this = this;
        let markerList = [];
        for (let i = 0; i < _this.markers.length; i++) {
            let marker = _this.markers[i];
            if(!marker){
                continue;
            }
            if (markerCodes.indexOf(marker.getMarkerCode().getCode()) !== -1){
                markerList.push(marker);
            }
        }
        return markerList;
    }

    putMarkerCodes(...markerCodes){
        let _this = this;
        markerCodes.forEach( m =>{
            if (_this.markerCodes.indexOf(m) == -1) {
                _this.markerCodes.push(m)
            }
        })
    }

        /**
     *
     * @param markers {Marker}
     */
        addMarker(...markers){
            let _this =this;
            for (let m of markers){
                _this.markers.push(m);
            }
        }
        /**
         * 通过id删除标绘
         * @param markerId {String}
         */
        removeMarkerById(markerId){
            let _this = this;
            if (markerId){
                let marker = _this.getMarkerById(markerId);
                if (marker){
                   _this.removeMarker(marker);
                }
            }
        }
        /**
         * 删除标绘
         * @param marker {Marker}
         */
        removeMarker(marker){
            let _this = this;
            if (marker){
                if (marker.isCompositeEntity){
                    marker.getEntity().destroy();
                }else{
                    _this.curViewer.entities.remove(marker.getEntity());
                }
                const number = _this.markers.indexOf(marker);
                if (number !== -1){
                    //判断默认监听器中，是否还有筛选的实体
                    const defaultMonitor = _this.monitorManager.getDefaultMonitor();
                    defaultMonitor.removeEntityfromEntities(marker.getEntity());
    
                    //如果开启了图层
                    if (_this.layerManager) {
                        let curlayer = undefined;
                        if (marker.getMerkerType() === commonConstants.markerType.text){
                            curlayer =_this.layerManager.getTextLayer();
                        }else{
                            curlayer = _this.layerManager.getCurLayer();
                        }
                        curlayer.remove(marker);
                    }
    
                    //属性面板
                    if (_this.attributePlane) {
                        //使属性面板关闭
                    }
    
                    _this.markers.splice(number,1);
                }
            }
        }

        /**
         * 通过markerCode 获得对应的控制器
         * @param markerCode 
         */
        getMonitorKey(markerCode){
            let _this = this;
            let monitorKey = markerCode.getMonitorKey();
            if (monitorKey){
                return monitorKey;
            }
            const markerType = markerCode.getMerkerType();
            if (markerType === commonConstants.markerType.point){
                monitorKey = commonConstants.monitorKey.JB;
            }else if (commonConstants.markerType.line){
                monitorKey = markerCode.getCode();
            }else if (markerType === commonConstants.markerType.text){
                monitorKey = "textMarker"
            }
            return monitorKey;
        }
       
        /**
         * 通过markerCOde的COde来获取对象
         */
        getMarkerCode(code){
            const _this = this;
            if(!code){
                return undefined;
            }
          
            if (code.hasOwnProperty("className")) {
                if (code.className == "MarkerCode") {
                    return code;
                }
            }
        
            let find  = undefined;
            for (let i = 0; i < _this.markerCodes.length; i++) {
                const element = _this.markerCodes[i];
                find = commonMethod.findElementFromArray(element.codes, mc=> mc.getCode() == code);
                if (find) {
                    break;
                }
            }
            return find;
        }

        /**
         * 标绘的代号，这个代号可以知道是点标和线标，并指定相应的控制器
         * @param code
         * @param params
         * @param callbackFunction
         * @return  标绘的id；
         */
        createMarker (code,params,callbackFunction) {
            let _this = this;
            let markerCode = _this.getMarkerCode(code);
            if(!markerCode){
                callbackFunction(false);
                return
            }
            
            if (!params) {
                params ={
                    markerId:commonMethod.creatUUID(),
                    camp:commonConstants.troopCamp.our_side,
                    markerCode:markerCode,
                    // showForm:commonConstants.jbShowForm.groundLayer,
                    markerName:markerCode.getMarkerName()
                };
            }

            let monitor = _this.monitorManager.getMonitorByKey(_this.getMonitorKey(markerCode));
            monitor.setParams(params);
            monitor.setFinishCallback( marker => {
                _this.addMarker(marker);
                //图层的管理
                if (_this.layerManager) {
                    let curlayer = undefined;
                    if (marker.getMerkerType() === commonConstants.markerType.text){
                        curlayer = _this.layerManager.getTextLayer();
                    }else{
                        curlayer = _this.layerManager.getCurLayer();
                    }
                    curlayer.addEntity(marker);
                    marker.setLayerId(curlayer.getLayerID());
                }
    
                //属性面板
                if (_this.attributePlane) {
                    //使属性面板的对象改变
                }

                //事件消除，不能连续标绘
                _this.monitorManager.deployDefaultMonitor();
    
                if (callbackFunction){
                    //如果有回调，就替换后置函数
                    callbackFunction(marker);
                }
    
            });

            _this.monitorManager.deployMonitor(monitor);
        }


        /**
         * 恢复标绘的显示
         * @param markerJson
         */
        reload(markerJson){
            //根据类型恢复不同的标绘

        }


        /**
         * 消除所有的对象
         */
        destroy(){
            let _this = this;
            for (let i = 0; i < _this.markers.length; i++) {
                let marker = _this.markers[i];
                marker.destroy(_this.curViewer);
            }
            _this.markers = [];
        }
};

export default MarkerManager;